﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TVGuide.DataAccess;
using System.Text.RegularExpressions;
using TVGuide.Common;
using TVGuide.DataAccess.Common;

namespace TVGuide.Parser
{
    class AraratTVParser : IChannelParser
    {
        public Guid ChannelId()
        {
            throw new NotImplementedException();
        }

        public string ChannelName()
        {
            return "Ararat";
        }

        public List<Program> ParseProgram()
        {
            return ParseProgram(DateTime.Today);
        }

        public List<Program> ParseProgram(DateTime date)
        {
            string programName = "";
            List<Program> tvProgramList = new List<Program>();
            Encoding enc = Encoding.ASCII;
            string URL = ChannelUrls.H1_ARM_URL + "/?date=" + date.Day + "-" + date.Month + "-" + date.Year;
            var h1String = HTTPClient.GetSiteContent(URL, enc, this.ChannelName());
            string pattern = "<table[^>]*id=sch_2.*?</table>";
            Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
            Match matche = rgx.Match(h1String);
            string capturedTable = matche.Value;
            string divMatcher = "<div>.*?</div>";
            Regex rgxDiv = new Regex(divMatcher, RegexOptions.IgnoreCase);
            MatchCollection divMatche = rgxDiv.Matches(capturedTable);
            List<string> temp1 = divMatche.removeHTMLTags();
            int hour;
            int minutes;
            foreach (string item in temp1)
            {
                Utils.seperateTimeProgramName(item, out hour, out minutes, out programName);
                Program tvProgram = new Program();
                tvProgram.Name = programName;
                tvProgram.Date = new DateTime(
                    date.Year,
                    date.Month,
                    date.Day,
                    hour,
                    minutes,
                    0
                    );
                tvProgram.ChannelId = (int)Enums.Channels.ARARAT;
                tvProgramList.Add(tvProgram);
            }

            return tvProgramList;
        }


        public List<Program> ParseProgramForWeek()
        {
            throw new NotImplementedException();
        }
    }
}
